//
//  BaseViewModel.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/13.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "BaseViewModel.h"
#import "BaseJSONEntity.h"
#import "ErrorCode.h"
#import "Message.h"

@implementation BaseViewModel

#pragma 接收传过来的block
- (void)setBlockWithReturnBlock:(ReturnValueBlock)returnBlock
                 WithErrorBlock:(ErrorCodeBlock)errorBlock
               WithFailureBlock:(FailureBlock)failureBlock
{
    _returnBlock = returnBlock;
    _errorBlock = errorBlock;
    _failureBlock = failureBlock;
}

- (void)getReturnBlockWithReturnBlock:(GetReturnBlock)returnBlock
                       WithErrorBlock:(GetErrorBlock)errorBlock
                        WithFailBlock:(GetFailBlock)failBlock
{
    _getReturnBlock = returnBlock;
    _getErrorBlock = errorBlock;
    _getFailBlock = failBlock;
}

// 后台正常返回时的统一处理函数
- (BaseJSONEntity *)fetchValueSuccessWithDic:(id)returnValue andUrl:(NSString *)url
{
    // 如果后台返回的不是字典类型，统一报错
    if (![returnValue isKindOfClass:[NSDictionary class]]) {
        return [[BaseJSONEntity alloc] initWithReturnCode:CODE_ERR_DATAFORMAT andMessage:MSG_ERR_DATAFORMAT];
    }
    BaseJSONEntity *respond = [BaseJSONEntity mj_objectWithKeyValues:returnValue];
    respond.url = url;
    if (0 != respond.ret) {
        if (self.errorBlock) {
            BaseJSONEntity *errorEntity = [[BaseJSONEntity alloc] initWithReturnCode:respond.ret andMessage:respond.msg];
            errorEntity.url = url;
            errorEntity.data = respond.data;
            self.errorBlock(errorEntity);
        }
        return nil;
    }
    
    if (self.returnBlock) {
        self.returnBlock(respond);
    }
    return respond;
}

// 网络请求错误处理
- (void)errorCodeWithDic:(NSError *)errorDic andUrl:(NSString *)url
{
    if (self.errorBlock) {
        NSLog(@"%ld", errorDic.code);
        BaseJSONEntity *errorEntity = [[BaseJSONEntity alloc] initWithReturnCode:CODE_NETWORK_INACCESSIBILITY andMessage:MSG_NETWORK_INACCESSIBILITY];
        if (CODE_AFN_TIMEOUT == errorDic.code) {
            errorEntity = [[BaseJSONEntity alloc] initWithReturnCode:CODE_AFN_TIMEOUT andMessage:NSLocalizedString(@"NetWorkFail_TimeOut", @"網絡異常，請稍後嘗試")];
        }
        errorEntity.url = url;
        self.errorBlock(errorEntity);
    }
}

// 网络状态异常处理
- (void)netFailureWithUrl:(NSString *)url
{
    if (self.failureBlock) {
        BaseJSONEntity *failureEntity = [[BaseJSONEntity alloc] initWithReturnCode:CODE_NO_NETWORKING andMessage:MSG_NO_NETWORKING];
        failureEntity.url = url;
        self.failureBlock(failureEntity);
    }
}

- (void)sendRequestWithUrl:(NSString *)url andParams:(NSMutableDictionary *)params
{
    WEAKSELF
    [[BaseJSONService share] postMethodSupportJSONWithUrl:url parameters:params success:^(id  _Nonnull data) {
        if (weakSelf && [weakSelf respondsToSelector:@selector(fetchValueSuccessWithDic:andUrl:)]) {
            [weakSelf fetchValueSuccessWithDic:data andUrl:url];
        }
    } error:^(NSError * _Nullable err) {
        if (weakSelf && [weakSelf respondsToSelector:@selector(errorCodeWithDic:andUrl:)]) {
            [weakSelf errorCodeWithDic:err andUrl:url];
        }
    } failure:^{
        if (weakSelf && [weakSelf respondsToSelector:@selector(netFailureWithUrl:)]) {
            [weakSelf netFailureWithUrl:url];
        }
    }];
}

- (void)getRequestWithUrl:(NSString *)url andParams:(NSMutableDictionary *)params
{
    WEAKSELF
    [[BaseJSONService share] getMethodSupportJSONWithUrl:url parameters:params success:^(id  _Nonnull data) {
        if (weakSelf.getReturnBlock) {
            weakSelf.getReturnBlock(data, url);
        }
    } error:^(NSError * _Nullable err) {
        NSString *error = err.userInfo[@"NSLocalizedDescription"];
        if (CODE_AFN_TIMEOUT == err.code) {
            error = NSLocalizedString(@"NetWorkFail_TimeOut", @"网络异常，请稍后尝试 ");
        }
        if (weakSelf.getErrorBlock) {
            weakSelf.getErrorBlock(error, url);
        }
    } failure:^{
        if (weakSelf.getErrorBlock) {
            weakSelf.getErrorBlock(MSG_NO_NETWORKING, url);
        }
    }];
}

@end
